package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.result.Result;
import com.dongdongshop.result.ResultEnum;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {

    private static final Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Reference
    private BrandService brandService;

    @RequestMapping(value = "toBrandShow")
    public String toBrandShow(){
        return "admin/brand";
    }

    //查看品牌
    @RequestMapping(value = "getListBrands",method = RequestMethod.POST)
    @ResponseBody
    public Result<PageUtil<TbBrand>> listBrands(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        try {
            PageInfo<TbBrand> pageInfo = brandService.listBrands(pageNumber,pageSize);
            PageUtil<TbBrand> pageUtil = new PageUtil<>();
            pageUtil.setPageNumber(pageNumber);
            pageUtil.setPageSize(pageSize);
            pageUtil.setList(pageInfo.getList());
            pageUtil.setTotal(pageInfo.getTotal());
            return Result.jsonResponse(ResultEnum.OK).setData(pageUtil);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "insertBrand",method = RequestMethod.POST)
    @ResponseBody
    public Result insertBrand(TbBrand tbBrand){
        if ( null == tbBrand.getName()|| "".equals(tbBrand.getName())
                || null == tbBrand.getFirstChar()|| "".equals(tbBrand.getFirstChar())){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            int i = brandService.insertBrand(tbBrand);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "toUpdateBrand/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Result<TbBrand>  toUpdateBrand(@PathVariable("id") Long id){
        if (null == id || "".equals(id)){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            TbBrand tbBrand = brandService.selectBrandById(id);
            return Result.jsonResponse(ResultEnum.OK).setData(tbBrand);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "updateBrand",method = RequestMethod.POST)
    @ResponseBody
    public Result updateBrand(TbBrand tbBrand){
        if ( null == tbBrand.getName()|| "".equals(tbBrand.getName())
                || null == tbBrand.getFirstChar()|| "".equals(tbBrand.getFirstChar())){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }

        try {
            int i = brandService.updateBrand(tbBrand);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "deleteBatch",method = RequestMethod.POST)
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]") Long[] ids){
        try {
            int i = brandService.deleteBrandBatch(ids);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }
}
